<script>

	function departmentOnChange(){

		var filter = {"is_active":1,"department_id":$("#department_id").val()};
		var data = {obj:"MTahunAjaran",filter: filter,value_field:"id",title_field:"name"};		
		renewSelectData(data, "#tahun_ajaran_id");

		$('#tblData').jqGrid('clearGridData');
		
		/*
		$("#tingkatan_id").val("");
		$("#kelas_tahun_ajaran_id_asal").val("");
		$("#kelas_tahun_ajaran_name_asal").val("");
		$('#tblData').jqGrid('clearGridData');
		$("#kelas_tahun_ajaran_id_tujuan").val("");
		$("#kelas_tahun_ajaran_name_tujuan").val("");
		$('#tblData2').jqGrid('clearGridData');
		*/
	}
	function tahunAjaranOnChange(){
		$('#tblData').jqGrid('clearGridData');
	}
	
	function kelasTahunAjaranOnBeforePopup(){
		var element = "<input type='text' name='tahun_ajaran_id' value='" + $("#tahun_ajaran_id").val() + "'/>";
		$('#frmFilter').html(element);		
	}
	function refreshGrid(){
		$("#tblData").jqGrid("setGridParam",{
			postData:{
				"kelas_tahun_ajaran_id" :$("#kelas_tahun_ajaran_id").val(),
			},
			datatype:"json"
		}).trigger("reloadGrid");
	}
	
	function getKelasTahunAjaran(obj){
		printConsole(obj);		
		oldValue = $("#kelas_tahun_ajaran_id").val();
		if (obj ===""){
			$("#kelas_tahun_ajaran_id").val("");
			$("#kelas_tahun_ajaran_name").val("");
		}else{
			$("#kelas_tahun_ajaran_id").val(obj.id);
			$("#kelas_tahun_ajaran_name").val(obj.kelas_name);
		}
		if (oldValue != $("#kelas_tahun_ajaran_id").val()){
			refreshGrid();
		}
	}
	
	function editRow(id){
		var url="index.php?r=akSiswa_Status/form&op=edit&id=" + id;
		var winName = "Form Process";
		var specs ="menubar=0,scrollbars=1,status=0,titlebar=0,toolbar=0";
		var width=450,height=300;
		var replace = true,onCenter = true;
		var w = openWindow(winName,url,width,height,onCenter,specs,replace);
		w.focus();
		var timer = setInterval(function() {   
			if(w.closed) {  
				clearInterval(timer);  
				reloadPage();
			}  
		}, 1000);
	}
	
		
	$(function(){
		var numberTemplate = {formatter: 'number', align: 'right', sorttype: 'number'};	 
		$("#tblData").jqGrid({
			url:'index.php?r=akademik/siswa/status/listDetail',
			editUrl:'index.php?r=akademik/siswa/status/edit',
			datatype: 'local',
			loadonce:true,
			mtype:"GET",
			autowidth:true,
			height:"100%",
			altRows:true,
			ignoreCase:true,
			//$strFields="id,department_id,sid,name,if(gender='P','Pria','Wanita') as gender,birth_date,address,home_phone,handphone,email,ayah_nama,ibu_nama,wali_nama,is_active";
			colNames: ['id','SID','Nama','Jenis Kelamin','Naik','Tinggal','Pindah','Keterangan'],
			colModel: [				
				{name: 'id', index: 'id', width: 20,hidden:false,sorttype:'int',formatter:'int'},
				{name: 'sid', index: 'sid', width: 40,search:true},
				{name: 'student_name', index: 'student_name', width: 150,search:true},
				{name: 'gender', index: 'gender', width: 60,search:true},
				{name: 'promote', index: 'promote', width: 30,formatter:'checkbox',align:'center',editable:true,edittype:'checkbox'},
				{name: 'not_promote', index: 'not_promote', width: 30,formatter:'checkbox',align:'center',editable:true,edittype:'checkbox'},
				{name: 'move', index: 'move', width: 30,formatter:'checkbox',align:'center',editable:true,edittype:'checkbox'},
				{name: 'status_notes', index: 'status_notes', width: 100,editable:true,edittype:'textarea'},
			],
			sortorder:"desc",
			gridview: true,
			rownumbers: false,
			rowNum: 10,
			rowList: [5, 10, 15],
			pager: '#navTblData',
			viewrecords: true,
			caption: 'Daftar Murid',
			cellEdit:true,
			cellsubmit:'clientArray',
			afterSaveCell: function(rowid, cellname, value, iRow, iCol){
				data = {"op": "edit","id":rowid,"colName":cellname,"value":value};				
				$.ajax({
					url:$("#tblData").jqGrid("getGridParam","editUrl"),
					type:"POST",
					async:false,
					data: data,
					success: function(data) {
						if(data.actionStatus == 0){
							if (value == "Yes"){
								if (cellname == "promote" || cellname == "not_promote" || cellname == "move"){
									$('#tblData').jqGrid("setCell",rowid,"promote","No");
									$('#tblData').jqGrid("setCell",rowid,"not_promote","No");
									$('#tblData').jqGrid("setCell",rowid,"move","No");
									$('#tblData').jqGrid("setCell",rowid,cellname,"Yes");
								}
							}
						}else{
							alert(data.actionMessage);
							$('#tblData').jqGrid("restoreCell",iRow,iCol);									
						}
					},					
				});
			},
			
			jsonReader : { 
				root:"model",
				repeatitems: false			
			},
			loadComplete:function(data){
				console.log(data);
			},			
		});
		$("#tblData").jqGrid('navGrid','#navTblData',{add:false,edit:false,search:false,del:false,refresh:false});	
	});
</script>

<form id="frmFilter" method="POST" style="display:none"></form>
<div style="width:1150px;margin-left:auto;margin-right:auto">
	<div>
		<label class="form" style="width:100px"><?php echo Yii::t("label","Department");?></label>
		<select id="department_id" name="department_id" onChange="departmentOnChange()">
			<?php
				foreach($arrDepartment as $rw){
					$selected = $department_id == $rw->id ? "selected" :"";				
					echo "<option value='".$rw->id ."' $selected >". $rw->name ."</option>";
				}
			?>
		</select>
	</div>
	<div style="margin-bottom:10px">
		<label class="form" style="width:100px"><?php echo Yii::t("label","Tahun Ajaran");?></label>
		<select id="tahun_ajaran_id" name="tahun_ajaran_id" onChange="tahunAjaranOnChange()">
			<?php
				foreach($arrTahunAjaran as $rw){
					$selected = $tahun_ajaran_id == $rw->id ? "selected" :"";				
					echo "<option value='".$rw->id ."' $selected >". $rw->name ."</option>";
				}
			?>
		</select>
				
	</div>
	
	
	<hr>
	<style>
		#gview_tblData{
			min-height:250px;
		}		
	</style>
	
	<div style="float:left;width:100%">
		<div style="margin-bottom:10px">
			<label class="form mandatory" style="width:100px"><?php echo Yii::t("label","Kelas")?></label>
			<input type="hidden" id="kelas_tahun_ajaran_id" name="kelas_tahun_ajaran_id" value="" size="2" />		
			<?php
				$this->widget('application.components.widgets.FindTextField',
					array('options' => array('id'=>'kelas_tahun_ajaran_name','value'=>'','disabled'=>''),
							'windowParams' => array('url'=>"index.php?r=data_popup/kelasTahunAjaran" ,
									'specs'=> array('height'=>300,'width'=>800),
									'name'=> "popKelasTahunAjaran",
							),
							'callback'=>"getKelasTahunAjaran",
							'beforePopupFunc'=>"kelasTahunAjaranOnBeforePopup",
							'formId'=>"frmFilter",
					)
				);
			?>
		</div>
			
		<table id="tblData"><tr></tr></table>
		<div id="navTblData"></div>
	</div>	
</div>